class Admin::SanPhamController < ApplicationController
  include ApplicationHelper
  def index
    @trang = (params[:page].present? && params[:page].to_i > 1 ?  params[:page] : 1).to_i
    @danh_muc = DanhMuc.find_by_slug(params[:danh_muc])
    @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus.select{ |v| v.ngon_ngu == I18n.locale.to_s }
    if (@danh_muc_ngon_ngu.empty?)
      @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus[0]
    else
      @danh_muc_ngon_ngu= @danh_muc_ngon_ngu[0]
    end

    if ['da_xoa'].include?(params[:loc])
      query = SanPham.where(:danh_muc_id => @danh_muc.id, :dang_kich_hoat => false)
    else
      query = SanPham.where(:danh_muc_id => @danh_muc.id, :dang_kich_hoat => true)
    end

    @san_phams = query.paginate(
      :order => :created_at.asc,
      :per_page => 20,
      :page => @trang
    )
    @co_trang_sau = !query.paginate(
      :order => :ten_hien_thi.asc,
      :per_page => 20,
      :page => @trang + 1
    ).empty?
    @co_trang_truoc = @trang > 1
    @index_dau_tien = (@trang - 1) * 20
  end

  def cap_nhat
    @nguoi_dung = nguoi_dung_hien_tai
    if (params[:commit])
      co_loi = false
      @san_pham = SanPham.find(params[:uid]) || not_found
      if params[:khuyen_mai_bat_dau].present?
        # co thoi gian khuyen mai
        if params[:khuyen_mai_ket_thuc].present?
          dinh_dang = "%d-%m-%Y"
          if DateTime::strptime(params[:khuyen_mai_bat_dau], dinh_dang) > DateTime::strptime(params[:khuyen_mai_ket_thuc], dinh_dang)
            flash.now[:loi] = t 'ngay_khuyen_mai_ket_thuc_phai_nho_hon_ngay_bat_dau'
          else
            @san_pham.khuyen_mai_bat_dau = params[:khuyen_mai_bat_dau]
            @san_pham.khuyen_mai_ket_thuc = params[:khuyen_mai_ket_thuc]
            @san_pham.gia_khuyen_mai = params[:gia_khuyen_mai].blank? ? nil : params[:gia_khuyen_mai]
          end
        else
          flash.now[:loi] = t 'ngay_khuyen_mai_ket_thuc_phai_nho_hon_ngay_bat_dau'
        end
      end
      @san_pham.hinh_anh = params[:hinh_anh]
      @san_pham.gia = params[:gia]
      if @san_pham.valid?
      @san_pham.save
      else
      co_loi = true
      end
      if co_loi
        flash.now[:loi] = t('kiem_tra_lai_thong_tin')
      end
    end
    @san_pham = SanPham.find(params[:uid]) || not_found
  end

  def xoa
    nguoi_dung = nguoi_dung_hien_tai()
    if nguoi_dung.present? && nguoi_dung.is_admin && params[:uid].present?
      san_pham = SanPham.find(params[:uid])
      if (san_pham.present?)
      san_pham.dang_kich_hoat = false;
      san_pham.save
      end
    end
    redirect_to :back
  end

  def khong_xoa
    nguoi_dung = nguoi_dung_hien_tai()
    if nguoi_dung.present? && nguoi_dung.is_admin && params[:uid].present?
      san_pham = SanPham.find(params[:uid])
      if (san_pham.present?)
      san_pham.dang_kich_hoat = true;
      san_pham.save
      end
    end
    redirect_to :back
  end

  def chi_tiet_nhom
  end

  def nhom_san_pham
  end

  def them_nhom
  end

  def ban_dich
    @san_pham = SanPham.find(params[:uid]) || not_found
    @ban_dich = @san_pham.noi_dung(params[:ngon_ngu])
    if params[:commit].present?
      if @ban_dich.present?
        @ban_dich.tieu_de = params[:tieu_de]
        @ban_dich.noi_dung = params[:noi_dung]
        if        !@ban_dich.save
          flash.now[:loi] = t 'kiem_tra_lai_thong_tin'
        end
      else
        bd = NoiDungSanPham.new(
        :tieu_de => params[:tieu_de],
        :noi_dung => params[:noi_dung],
        :nguoi_dung => @nguoi_dung,
        :ngon_ngu => params[:ngon_ngu]
        )
        if bd.valid?
          @ban_dich = bd
        @san_pham.noi_dung_san_phams << @ban_dich
        @san_pham.save
        flash.now[:thanh_cong] = t 'ban_dich_da_cap_nhat'
        else
          flash.now[:loi] = t 'kiem_tra_lai_thong_tin'
        end
      end

    end
  end

  def kich_hoat_ban_dich
    nguoi_dung = nguoi_dung_hien_tai()
    if nguoi_dung.present? && nguoi_dung.is_admin && params[:uid].present? && params[:ngon_ngu].present?
      san_pham = SanPham.find(params[:uid])
      if (san_pham.present?)
        sp_ngon_ngu = san_pham.noi_dung(params[:ngon_ngu])
        if sp_ngon_ngu.present?
        sp_ngon_ngu.dang_kich_hoat = true;
        san_pham.save
        end
      end
    end
    redirect_to :back
  end

  def vo_hieu_ban_dich
    nguoi_dung = nguoi_dung_hien_tai()
    if nguoi_dung.present? && nguoi_dung.is_admin && params[:uid].present? && params[:ngon_ngu].present?
      san_pham = SanPham.find(params[:uid])
      if (san_pham.present?)
        sp_ngon_ngu = san_pham.noi_dung(params[:ngon_ngu])
        if sp_ngon_ngu.present?
        sp_ngon_ngu.dang_kich_hoat = false;
        san_pham.save
        end
      end
    end
    redirect_to :back
  end

  def tao_moi
    @danh_muc = DanhMuc.find_by_slug(params[:danh_muc])
    @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus.select{ |v| v.ngon_ngu == I18n.locale.to_s }
    if (@danh_muc_ngon_ngu.empty?)
      @danh_muc_ngon_ngu = @danh_muc.danh_muc_ngon_ngus[0]
    else
      @danh_muc_ngon_ngu= @danh_muc_ngon_ngu[0]
    end
    @nguoi_dung = nguoi_dung_hien_tai
    if (params[:commit])
      co_loi = false
      noi_dung_sp = NoiDungSanPham.new(
      :tieu_de => params[:tieu_de],
      :noi_dung => params[:noi_dung],
      :nguoi_dung => @nguoi_dung,
      :ngon_ngu => params[:ngon_ngu]
      )
      if noi_dung_sp.valid?
        san_pham = SanPham.create({
          :slug => params[:slug],
          :hinh_anh => params[:hinh_anh],
          :gia => params[:gia].to_i,
          :nguoi_dung => @nguoi_dung,
          :danh_muc => @danh_muc
        })
        if !san_pham.present?
        co_loi = true
        else
        san_pham.noi_dung_san_phams << noi_dung_sp
        san_pham.save
        end
      else
      co_loi = true
      end
      if co_loi
        flash.now[:loi] = t('kiem_tra_lai_thong_tin')
      else
        redirect_to url_for(:action => 'index')
      end
    end
  end
end
